<?php


namespace App\Http\Controllers\Api;
use App\Http\Controllers\BaseController;
use App\Models\Balance;
use App\Models\Config;
use App\Models\CookbookAdvView;
use App\Models\Focus;
use App\Models\User;
use Illuminate\Http\Request;

class UserController extends BaseController
{

    public function index(Request $request)
    {
        $this->validate($request, [
           'user_id' => 'required|exists:users,id'
        ]);
        $cur_user_id = $this->getUserId();
        $id = \request('user_id');
        $info = User::withCount('focus', 'fans')->withCount(['stared' => function($q) {
            $q->where('type', 2);
        }])->withCount(['is_focus' => function($q) use($cur_user_id){
            $user_id = $cur_user_id;
            $q->where('user_id', $user_id);
        }])->find($id);

        if ($id != $cur_user_id) {
            $info->setHidden(['balance', 'is_vip', 'vip_expired_at', 'mobile', 'openid']);
        }

        if ($id == $cur_user_id) {
            $max_date = Balance::where('parent_user_id', $id)->where('type', Balance::TYPE_COOKBOOK_VIDEO)->max('extra');
            $total = CookbookAdvView::where('merchant_id', $id)->where('benefit_user', 2)->when($max_date, function ($q) use($max_date){
                $q->where('month', '>', $max_date);
            })->where('is_finish', 1)->count();
            $info->total_adv_income = Config::getAdvUnit() * $total;
            $info->total_income_yuan = format_yuan(Balance::getTotalIncome($id, 2));
        }
        return $this->success($info);
    }

    public function fans(Request $request) {
        $this->validate($request, [
            'user_id' => 'required'
        ]);
        $cur_user_id = $this->getUserId();
        $id = \request('user_id');
        $list = Focus::where('target_user_id', $id)->withCertain('fans', ['nickname', 'avatar_url'])->orderBy("id", "desc")
            ->paginate(\request('per_page', 20));
        foreach ($list as &$item) {

            //判断我是否关注他
            $exist = Focus::where('user_id', $cur_user_id)->where('target_user_id', $item->user_id)->exists();
            $item->is_focus = $exist ? 1 : 0;

        }
        return $this->paginate($list);
    }

    public function focus(Request $request) {
        $this->validate($request, [
            'user_id' => 'required'
        ]);
        $cur_user_id = $this->getUserId();
        $id = \request('user_id');
        $list = Focus::where('user_id', $id)->withCertain('focus', ['nickname', 'avatar_url'])->orderBy("id", "desc")
            ->paginate(\request('per_page', 20));
        foreach ($list as &$item) {

            //判断我是否关注他
            $exist = Focus::where('user_id', $cur_user_id)->where('target_user_id', $item->target_user_id)->exists();
            $item->is_focus = $exist ? 1 : 0;

        }
        return $this->paginate($list);
    }

    public function qrcode() {
        $app = $this->miniProgram();
        $user_id = $this->getUserId();
        $fileName = 'appcode_' . $user_id . '.png';
        if (file_exists('qrcode/' . $fileName)) {
            return $this->success([
                'url' => env('APP_URL') . '/qrcode/' . $fileName
            ]);
        } else {
            $url = '/pages/transfer/index?merchant_id=' . $user_id;
            $response = $app->app_code->getQrCode($url);
            if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
                $fileName = $response->saveAs('qrcode', $fileName);
                return $this->success([
                    'url' => env('APP_URL') . '/qrcode/' . $fileName
                ]);
            } else {
                $this->failed('生成二维码失败');
            }
        }


    }

}

